package com.think.filter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

public class MyFilter implements Filter {

    public void init(FilterConfig config) {
        /*
          idea main:C:\Users\Administrator\AppData\Local\Temp\tomcat-docbase.124697043038427371.9999\
          idea war:E:\workspace\think-projects\thinkspringboot\think-web\target\think-web\
          tomcat war:D:\soft\dev\server\tomcat\tomcat-8.5.16 - test\bin
          jar:E:\workspace\think-projects\thinkspringboot\think-web\src\main\webapp\
         */
        /*String webroot = config.getServletContext().getRealPath("/");
        System.err.println("webroot:" + webroot);
        String rootDir = FileUtil.getRootDir();
        File resourcePath = new File(rootDir + File.separator + "resource");
        System.out.println("resourcePath:" + resourcePath);
        if (!resourcePath.exists()) {
            resourcePath.mkdirs();
        }
        try {
            File file = new File(resourcePath.getAbsolutePath() + File.separator + "1.txt");
            FileUtils.writeStringToFile(file, new Date().toString(), StandardCharsets.UTF_8, false);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        //TestLoad.start();
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        if (servletRequest != null) {
            //throw new RuntimeException("filter error 不会被ExceptionHandle拦截，需要单独处理。");
        }
       /* if (servletResponse != null) {
            HttpServletResponse response = (HttpServletResponse) servletResponse;
            response.sendRedirect("http://www.baidu.com");
            return;
        }*/
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
